package com.keda.service;

import com.keda.dao.ProductDao;
import com.keda.model.PageInfo;
import com.keda.model.Product;
import com.keda.model.ProductDetail;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

public class ProductService {


    private static final int PAGE_SIZE = 8;

    public PageInfo<Product> search(String bigTypeId, String smallTypeId,
                                 String keyword, String page) {

        try {
            Integer.parseInt(page);
        } catch (NumberFormatException e) {
            page = "1";
        }

        ProductDao productDao = new ProductDao();
        // 先查询页面的总页数（总数据/每页数据-向上取整）

        // 总数据
        int totalCount =  productDao.count(bigTypeId, smallTypeId,
                keyword);

        if (totalCount < 1) {
            return new PageInfo<>(Integer.parseInt(page),
                    0, new ArrayList<>());
        }

        List<Product> products = new ArrayList<>();

        if (totalCount > 0) {
            // 查询每页的商品
            products = productDao.query(bigTypeId, smallTypeId,
                    keyword, page);
        }

        // 构建分页结果对象
        PageInfo<Product> pageInfo = new PageInfo<>(Integer.parseInt(page),
                totalCount, products);

        return pageInfo;
    }

    public ProductDetail findById(String productId) {

        // 商品ID必须要有
        if (productId == null || productId.trim().isEmpty()) {
            throw new RuntimeException("请选择需要查看的商品！");
        }
        int productIdInt = 0;
        try {
            productIdInt = Integer.parseInt(productId);
        } catch (NumberFormatException e) {
            throw new RuntimeException("请选择需要查看的商品！");
        }

        ProductDao productDao = new ProductDao();
        ProductDetail productDetail = productDao.queryById(productIdInt);
        if (productDetail == null) {
            throw new RuntimeException("商品信息不存在，请返回！");
        }
        return productDetail;


    }

    /**
     * 添加历史记录
     * @param productDetail
     * @param session
     */
    public void addToHistory(ProductDetail productDetail, HttpSession session) {
        // 将用户查看的商品添加到浏览历史中(把数据存放到在某个容器中 -- 内存或数据库)
        // 本项目先存放Session中
        List<Product> userQueryHistories = (List<Product>)session.getAttribute("userQueryHistory");
        List<Product> showHistories = new ArrayList<>();
        if (userQueryHistories == null) {
            userQueryHistories = new ArrayList<>();
        }

        Product product = new Product();
        product.setId(productDetail.getId());
        product.setName(productDetail.getName());
        product.setPrice(productDetail.getPrice());
        product.setProPic(productDetail.getProPic());
        userQueryHistories.add(product);
        showHistories.add(product);
        session.setAttribute("userQueryHistory", userQueryHistories);

        if (userQueryHistories.size() > 1) {
            showHistories.add(userQueryHistories.get(userQueryHistories.size() - 2));
        }

        session.setAttribute("showHistories", showHistories);
    }
}
